package com.hdugraduate;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

public class AccountLogin extends Activity{
	private EditText flikrAccountEditText;
	String tempStr = null;
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		File rAccountFile = new File("sdcard/TN/Account.txt");
		try {		
		BufferedReader br = new BufferedReader(new FileReader(rAccountFile));
		tempStr = br.readLine();
		br.close();
		} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}// read a line
		
		setContentView(R.layout.accountlogin);
		flikrAccountEditText = (EditText) findViewById(R.id.accountInput);
		flikrAccountEditText.setText(tempStr);
	}
	
	public boolean onCreateOptionsMenu(Menu menu){
		menu.add(Menu.NONE,0,0,"Save");
		return super.onCreateOptionsMenu(menu);
	}
	
	public boolean onOptionsItemSelected(MenuItem item){
    	super.onOptionsItemSelected(item);
    	switch(item.getItemId()){
    		case 0:
    			try{
    				File wAccountFile = new File("sdcard/TN/Account.txt");
    				BufferedWriter bw = new BufferedWriter(new FileWriter(wAccountFile,false));
    				String flickrAccountString = flikrAccountEditText.getText().toString();
    				bw.append(flickrAccountString);
    				bw.newLine();
    				bw.flush();
    				bw.close();
    				Toast.makeText(AccountLogin.this,"Saved Success", Toast.LENGTH_SHORT).show();
    			}catch(Exception e){Toast.makeText(AccountLogin.this,"Saved Failed", Toast.LENGTH_SHORT).show();}
    			break;
    	}
    	return true;
    }
}
